package com.ittx.dzt.meituan.handler;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ittx.dzt.business.PlatformConstant;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.handler.AbstractBasePlatformApiHandler;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.mq.core.queue.producer.RocketmqProducer;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.sankuai.meituan.shangou.open.sdk.domain.SystemParam;
import com.sankuai.meituan.shangou.open.sdk.request.SgOpenGeneralRequest;
import com.sankuai.meituan.shangou.open.sdk.response.SgOpenResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 美团平台API处理父类
 *
 * @author gywang
 * @since 2024-07-30
 */
@Slf4j
public abstract class AbstractBaseMeituanApiHandler<T> extends AbstractBasePlatformApiHandler<T> {

    // 取值字段
    public static final String DATA_FIELD = "data";
    @Autowired
    protected RocketmqProducer rocketmqProducer;

    @Override
    public String getPlatformCode() {
        return PlatformConstant.MEITUAN;
    }

    /**
     * 执行API请求通用方法
     *
     * @param storeConfig 店铺配置
     * @param paramMap    请求参数
     * @param requestId   请求id
     * @return 请求结果
     */
    public Result executeMeituanApi(StoreConfigDTO storeConfig, Map<String, String> paramMap, String requestId) {

        // 发起请求
        Result result = null;
        try {
            SystemParam systemParam = new SystemParam(storeConfig.getAppKey(), storeConfig.getAppSecret());
            SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(getMeituanMethod(), getHttpMethod(), systemParam);
            sgOpenGeneralRequest.setParamMap(paramMap);

            SgOpenResponse sgOpenResponse = sgOpenGeneralRequest.doRequest();
            String returnMsg = sgOpenResponse.getRequestResult();
            log.debug(getMeituanMethod() + " 返回数据：");
            log.debug(returnMsg);
            if (StrUtil.isEmpty(returnMsg) || !JSONUtil.isJson(returnMsg)) {
                String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0012_MSG, requestId, this.getPlatformCode()
                        , this.getMeituanMethod(), sgOpenGeneralRequest.getUri(), returnMsg);
                log.error(msg);
                return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0012, msg));
            }
            Map<String, Object> resultMap = JacksonTool.toMap(returnMsg);
            Integer code = (Integer) resultMap.get("code");
            if (code == null || code != 0) {
                String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, requestId, this.getPlatformCode(), this.getMeituanMethod(), returnMsg);
                log.error(msg);
                return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg)).addMsg(JacksonTool.toJson(resultMap.get("error")));
            }
            return Result.success().addData(resultMap);
        } catch (Exception e) {
            String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, requestId, this.getPlatformCode(), this.getMeituanMethod(), e.getMessage());
            ExceptionLog.error(e, PlatformMessageConstant.MSG_PLAT_0003_MSG, requestId, this.getPlatformCode(), this.getMeituanMethod(), e.getMessage());
            return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg)).addMsg(msg);

        }

    }

    /**
     * 获取美团平台方法名称
     *
     * @return 美团平台API
     * @date 2022/5/5 15:01
     */
    public abstract String getMeituanMethod();

    /**
     * 接口调用http方法
     * POST、GET
     *
     * @return
     */
    public abstract String getHttpMethod();

    /**
     * 获取店铺id
     *
     * @param platformApiParamDTO
     * @return
     */
    public String getPoIId(PlatformApiParamDTO platformApiParamDTO) {
        return platformApiParamDTO.getStoreConfig().getStoreUniqueCode();
    }

    /**
     * 获取总页数
     *
     * @param totalCount 总记录数
     * @param pageSize   每页显示记录数
     * @return 总页数
     */
    public Integer totalPage(Integer totalCount, Integer pageSize) {
        if (totalCount <= pageSize) {
            return 1;
        }
        if (totalCount % pageSize == 0) {
            return totalCount / pageSize;
        }
        return (totalCount / pageSize) + 1;
    }

    public Long totalPage(Long totalCount, Long pageSize) {
        if (totalCount <= pageSize) {
            return 1L;
        }
        if (totalCount % pageSize == 0) {
            return totalCount / pageSize;
        }
        return (totalCount / pageSize) + 1;
    }

}
